<?php

class GL_Profile extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->layout->setLayout("home/layout");
    }

//    function index($id){
//        $data['current_user'] =  $this->user->get($id);
//        $this->layout->view('home/profile', $data);
//    }

    function index()
    {
        $uid = $this->session->userdata('logged_in')['id'];
        $data['current_user'] = $this->user->get($uid);

        $data['income'] = "";
        $year = date('Y');
        for ($i = 1; $i < 13; $i++) {
            if ($this->user->get_income($uid, $i, $year)['sum(purchasing_price)'] == null) {
                $income = 0;
            } else $income = $this->user->get_income($uid, $i, $year)['sum(purchasing_price)'] * 0.7;
            $data['income'] .= $income . ",";
        }

        $data['income'] = substr($data['income'], 0, strlen($data['income']) - 1);
        $this->layout->view('home/profile', $data);
    }

    public function update_info()
    {
        $data = array('name' => $this->input->post('name'),
            'email' => $this->input->post('email'), 'tel' => $this->input->post('tel'));
        echo $this->user->update($this->input->post('id'), $data);
    }

    function update_password()
    {
        $old = $this->session->userdata('logged_in')['password'];
//        echo $old;
        if (MD5($this->input->post('old_pw')) == $old) {
            $data = array('password' => MD5($this->input->post('password')));
            echo $this->user->update($this->input->post('id'), $data);
        } else echo 0;
    }

    public function upgrade()
    {
        $email = $this->session->userdata('logged_in')["id"];
        if ($this->user->edit_permission($email, 2)) {
            echo 1;
            $sess = $this->session->userdata('logged_in');
            $sess['permission'] = 2;
            $this->session->set_userdata('logged_in', $sess);
        }
    }

    function purchased()
    {
        $page = $this->input->get('page');
        $sort = $this->input->get('sort');
        $cat = $this->input->get('cat');
        $uid = $this->input->get('uid');
        $query = $this->input->get('q');
        $tag = $this->input->get('tag');
        if ($page == null) $page = 0;
        if ($sort == null) $sort = 1;
        if ($cat == null) $cat = 0;
        if ($uid == null) $uid = 0;
        if ($query == null) $query = '';
        if ($tag == null) $tag = 0;

        $puid = $this->session->userdata('logged_in')['id'];

        $data['products'] = $this->product->get_purchased($sort, $cat, $uid, $query, $tag, $puid);
        $data['page'] = $page;
        $data['sort'] = $sort;
        $data['cat'] = $cat;
        $data['uid'] = $uid;
        $data['query'] = $query;
        for ($i = $page * 25; $i < count($data['products']) && $i < $page * 25 + 25; $i++) {
            $data['images'][$i] = $this->product->get_images($data['products'][$i]->id);
        }
        $this->layout->view("home/index", $data);
    }

    function pending()
    {
        $page = $this->input->get('page');
        $sort = $this->input->get('sort');
        $cat = $this->input->get('cat');
        $uid = $this->session->userdata('logged_in')['id'];
        $query = $this->input->get('q');
        $tag = $this->input->get('tag');
        if ($page == null) $page = 0;
        if ($sort == null) $sort = 1;
        if ($cat == null) $cat = 0;
        if ($uid == null) $uid = 0;
        if ($query == null) $query = '';
        if ($tag == null) $tag = 0;

        $data['products'] = $this->product->get_uploaded_pending($sort, $cat, $uid, $query, $tag);
        $data['page'] = $page;
        $data['sort'] = $sort;
        $data['cat'] = $cat;
        $data['uid'] = $uid;
        $data['query'] = $query;
        for ($i = $page * 25; $i < count($data['products']) && $i < $page * 25 + 25; $i++) {
            $data['images'][$i] = $this->product->get_images($data['products'][$i]->id);
        }

        $this->layout->view("home/index", $data);
    }

    function censored()
    {

        $page = $this->input->get('page');
        $sort = $this->input->get('sort');
        $cat = $this->input->get('cat');
        $uid = $this->session->userdata('logged_in')['id'];
        $query = $this->input->get('q');
        $tag = $this->input->get('tag');
        if ($page == null) $page = 0;
        if ($sort == null) $sort = 1;
        if ($cat == null) $cat = 0;
        if ($uid == null) $uid = 0;
        if ($query == null) $query = '';
        if ($tag == null) $tag = 0;

        $data['products'] = $this->product->get_all_product($sort, $cat, $uid, $query, $tag);
        $data['page'] = $page;
        $data['sort'] = $sort;
        $data['cat'] = $cat;
        $data['uid'] = $uid;
        $data['query'] = $query;
        for ($i = $page * 25; $i < count($data['products']) && $i < $page * 25 + 25; $i++) {
            $data['images'][$i] = $this->product->get_images($data['products'][$i]->id);
        }

        $this->layout->view("home/index", $data);
    }
}